package com.showgood.socket;

import java.io.IOException;

import org.apache.log4j.Logger;

import flash.utils.ByteArray;

public class SocketCommandFactoryImpl implements ISocketCommandFactory {

	private Logger logger = Logger.getLogger(this.getClass());
	
	@SuppressWarnings("rawtypes")
	@Override
	public AbstractSocketCommand unpack(ByteArray byteArray) {
		try {
			int stamp = byteArray.readInt();
			int flag = byteArray.readShort();
			AbstractSocketCommand sc = null;
			if ((flag & ISocketCommand.FLAG_SINGLE_VO) != 0) {
				sc = new SingleSocketCommand(stamp, flag);
			}
			else if ((flag & ISocketCommand.FLAG_ARRAY_VO) != 0) {
				sc = new ArraySocketCommand(stamp, flag);
			}
			sc.unpack(byteArray);
			return sc;
		} catch (IOException e) {
			logger.error(e);
			return null;
		}
	}

	@Override
	public void pack(ISocketCommand sc, ByteArray byteArray) {
		try {
			sc.pack(byteArray);
		} catch (IOException e) {
			logger.error(e);
		}
	}

}


